﻿using System.Collections.Generic;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Data.SqlClient;
using System.Globalization;

namespace sistemaAcademicoFinanceiroEscolas
{
    public partial class vincularAnoLetivo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnVincular_Click(object sender, EventArgs e)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            string select = "select codigoAnoLetivo, codigoAula, ";
            select += "(select descricaoAnoLetivo from anoLetivo where SUBSTRING(aula.dataAula, 7, 4) = descricaoAnoLetivo) as anoLetivo, ";
            select += "(select codigoAnoLetivo from anoLetivo where SUBSTRING(aula.dataAula, 7, 4) = descricaoAnoLetivo) as anoLetivoCodigo ";
            select += "from aula";

            SqlCommand cmd = new SqlCommand(select, sqlc);
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    string update = "update aula set codigoAnoLetivo = " + dr["anoLetivoCodigo"].ToString() + " where codigoAula = " + dr["codigoAula"].ToString();

                    SqlCommand cmdUpdate = new SqlCommand(update, sqld);
                    cmdUpdate.ExecuteNonQuery();
                }
            }
            cmd.Dispose();
            dr.Close();

            sqlc.Close();
            sqld.Close();
        }
    }
}
